<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/9 0009
 * Time: 15:27
 */

namespace App\Models\Admin;


use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;

class PosOrder extends Common
{
    /**
     * POS订单表
     * @var string
     */
    protected $table = 'pos_order';

    /**
     * 获得POS订单表列表
     *
     * @param $sid
     * @param array $where
     * @return mixed
     */
    public function getLists($sid, $where = [])
    {
        if (empty($where['sort'])) {
            $sort = 'desc';
        } else {
            $sort = $where['sort'];
        }
        if (empty($where['pagesize'])) {
            $pageSize = 10;
        } else {
            $pageSize = $where['pagesize'];
        }
        //状态 0-已删除  1-待支付  2-已支付
        $query = $this->where('shop_node_id', $sid)
            ->where('status', 2)
            ->orderBy('id', $sort);
        if (!empty($where)) {
            if (!empty($where['s_time'])) {
                $query->where('create_time', '>=', $where['s_time']);
            }
            if (!empty($where['e_time'])) {
                $query->where('create_time', '<=', $where['e_time']);
            }
            if(!empty($where['order_type'])){
                $query->where('order_type',$where['order_type']);
            }
            if (!empty($where['keyname']) and !empty($where['keyval'])) {
                switch ($where['keyname']) {
                    case 'order_no':
                        $query->where('order_no', $where['keyval']);
                        break;
                    case 'mobile':
                        $query->where('mobile', $where['keyval']);
                        break;
                    default;
                }
            }
        }
        if (isset($where['export']) and $where['export'] == 'true') {
            if ($where['details_list']) {
                $query->whereIn('id', explode(',', $where['details_list']));
            }
            return $query->get();
        }
        return $query->paginate($pageSize);
    }

    /**
     * 通用导出EXCEL
     *
     * @param $fileName
     * @param array $firstRow
     * @param array $content
     */
    public static function exportFileNormal($fileName, array $firstRow, array $content)
    {
        Excel::create($fileName, function ($excel) use ($firstRow, $content) {
            $excel->sheet('导出表', function ($sheet) use ($firstRow, $content) {
                $sheet->rows(array(
                    $firstRow
                ));
                //同时增加多行
                $sheet->rows($content);
                $sheet->setAutoSize(true);
            });
        })->download('xls');
        exit();
    }

    /**
     * 将集合格式化成数组
     *
     * @param $query
     * @return array
     */
    public static function parseToExcel($query)
    {
        $data = [];
        foreach ($query as $k => $v) {
            if($v->order_type==1){
                //线下 有折扣
                $data[] = [
                    $k + 1,
                    $v->order_no,
                    '线下POS',
                    $v->mobile,
                    $v->create_time,
                    floatval($v->amount),
                    floatval($v->deposit),
                    floatval($v->deposit),
                    floatval($v->amount) * 0.006,
                    floatval($v->amount)-floatval($v->deposit) - floatval($v->amount) * 0.006
                ];
            }else{
                //报单 无折扣
                $data[] = [
                    $k + 1,
                    $v->order_no,
                    '报单',
                    $v->mobile,
                    $v->create_time,
                    floatval($v->amount),
                    0,
                    floatval($v->deposit),
                    floatval($v->amount) * 0.006,
                    floatval($v->amount) - floatval($v->amount) * 0.006
                ];
            }
        }
        return $data;
    }

    /**
     * 获取总营业额
     *
     * @param $sid
     * @return mixed
     */
    public function getIncome($sid)
    {
        $res = $this->where('shop_node_id', $sid)
            ->where('status', 2)
            ->sum('amount');
        return floatval($res);
    }

    /**
     * 应结算总额之和
     *
     * @param $sid
     * @return float
     */
    public function getRealIncome($sid)
    {
        $res = $this->where('shop_node_id', $sid)
            ->where('status', 2)
            ->sum('real_income');
        return floatval($res);
    }

    /**
     * 获取订单数
     *
     * @param $sid
     * @param $startDate
     * @param $endDate
     * @return array
     */
    public function getOrderNumByTimeRange($sid, $startDate, $endDate)
    {
        $where = [$startDate, $endDate];
        $regTrendList = DB::table('pos_order')->where('shop_node_id', $sid)
            ->select(DB::raw("to_char(create_time, 'yyyy-mm-dd') as create_time"))
            ->where('status', 2)
            ->whereBetween('create_time', $where)->get()->toArray();
        return Tools::getGroupCountByAttr($regTrendList, 'create_time');
    }

    /**
     * 获取订单金额
     *
     * @param $sid
     * @param $startDate
     * @param $endDate
     * @return array
     */
    public function getOrderPayAmountByTimeRange($sid, $startDate, $endDate)
    {
        $whereBetween = [$startDate, $endDate];
        $regTrendList = DB::table('pos_order')->where('shop_node_id', $sid)
            ->select(DB::raw("to_char(create_time, 'yyyy-mm-dd') as create_time,amount"))
            ->whereBetween('create_time', $whereBetween)
            ->where('status', 2)
            ->get()->toArray();
        return Tools::getGroupCountByAttr($regTrendList, 'create_time', 'amount');
    }

}